package com.tanhua.app.controller;

import com.aliyuncs.exceptions.ClientException;
import com.tanhua.app.service.UserService;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class UserInfoController {

    @Autowired
    private UserService userService;

    @GetMapping(value = "/users",name = "回显个人用户信息")
    public ResponseEntity login(Long userID,Long huanxinID,@RequestHeader("Authorization") String token) throws ClientException {


        //1 根据userID做查询
        if(userID!=null){
            return userService.findUserInfoVo(userID);
        }else if(huanxinID!=null){
            //2 根据huanxinID做查询
            return userService.findUserInfoVo(huanxinID);
        }else{
            //3 在根据tonken中的user.getId做查询
            User user = userService.tokenfromUser(token);
            return userService.findUserInfoVo(user.getId());
        }
    }


    @PostMapping(value = "/users/header",name = "修改我的头像")
    public ResponseEntity updatePic(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws ClientException, IOException {
        return userService.loginReginfoPic(headPhoto,token);
    }


    @PutMapping(value = "/users",name = "修改我的信息")
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) throws ClientException, IOException {
        return userService.updateUserInfo(userInfo,token);
    }


    @GetMapping(value = "/users/counts",name = "互相喜欢，喜欢，粉丝 - 统计")
    public ResponseEntity counts() throws ClientException, IOException {
        return userService.counts();
    }

    @GetMapping(value = "/users/friends/{type}",name = "互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表")
    public ResponseEntity findMyCount(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize,
                                      @PathVariable("type") Integer type) throws ClientException, IOException {
        return userService.findMyCount(page,pagesize,type);
    }


    @PostMapping(value = "/users/fans/{uid}",name = "添加喜欢变成互相喜欢")
    public ResponseEntity fansToFriend(@PathVariable("uid") Long fansId) throws ClientException, IOException {
        return userService.fansToFriend(fansId);
    }

    @DeleteMapping(value = "/users/like/{uid}",name = "取消喜欢")
    public ResponseEntity removeLike(@PathVariable("uid") Long uid) throws ClientException, IOException {
        return userService.removeLike(uid);
    }




}
